package crons;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admin.data.DomainData;

import models.DomainModel;
import models.OwnerDomainModel;

import data.OwnerDomainData;
import debugs.OwnerDomainDebug;

public class ConfirmDomainCron extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		DomainData domainData = new DomainData();
		OwnerDomainData ownerData = new OwnerDomainData();
		ArrayList<OwnerDomainModel> ownerDomains = ownerData.getVerifyDomainChange();
		
		for (int i = 0; i < ownerDomains.size(); i++) {
			//Update domain info
        	DomainModel domain = domainData.get(ownerDomains.get(i).getDomain());
        	boolean isChange = false;
        	if (!ownerDomains.get(i).getTitle().isEmpty()) {
        		isChange = true;
        		domain.setTitle(ownerDomains.get(i).getTitle());
        	}
        	
        	if (!ownerDomains.get(i).getDescription().isEmpty()) {
        		isChange = true;
        		domain.setDescription(ownerDomains.get(i).getDescription());
        	}
        	
        	if (isChange) {
        		domainData.insert(domain);
        	}
        	
        	//Update onwer domain
        	ownerDomains.get(i).setIsUpdated(1);
        	ownerData.insert(ownerDomains.get(i));
        	OwnerDomainDebug.debugWeb(ownerDomains.get(i), response);
		}
	}
}
